package stream;

import pojo.UserInfo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对集合元素进行多条件排序
 */
public class SoredAndComparator {
    public static void main(String[] args) {
        ArrayList<UserInfo> list = new ArrayList();
        list.add(new UserInfo(1, "张三", 1));
        list.add(new UserInfo(2, "李四", 2));
        list.add(new UserInfo(3, "王五", 3));
        list.add(new UserInfo(3, "赵六", 4));
        List<UserInfo> collect = list.stream()
                .sorted(Comparator
                        .comparing(UserInfo::getId)
                        .thenComparing(UserInfo::getIsDelete, Comparator.reverseOrder())  // 默认升序,多一个参数实现此字段倒序
                        // .reversed() // 实现所有条件排序后，再反转排序
                )
                .collect(Collectors.toList());
        collect.forEach(k -> {
            System.out.println(String.format("id=%s,name=%s,isDelete=%s", k.getId(), k.getName(), k.getIsDelete()));
        });
    }
}
